package com.leetcode.contest;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * 6079. 价格减免
 */
public class Solution1 {
    static class Solution {
        public String discountPrices(String sentence, int discount) {
            String[] split = sentence.split(" ");

            StringBuilder result = new StringBuilder();

            for (String s : split) {
                if (is(s)) {
                    String substring = s.substring(1);
                    long i = Long.parseLong(substring);

                    BigDecimal divide = BigDecimal.valueOf(discount).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);

                    BigDecimal valueOf = BigDecimal.valueOf(i);

                    BigDecimal multiply = valueOf.multiply(divide).setScale(2, RoundingMode.HALF_UP);

                    BigDecimal subtract = valueOf.subtract(multiply).setScale(2, RoundingMode.HALF_UP);

                    String value = subtract.toString();

                    result.append("$").append(value);
                } else {
                    result.append(s);
                }
                result.append(" ");
            }

            return result.substring(0, result.length() - 1);
        }

        /**
         * 是否是价格
         *
         * @param s
         * @return
         */
        private boolean is(String s) {
            if (s.length() < 2 || s.charAt(0) != '$') {
                return false;
            }

            for (int i = 1; i < s.length(); i++) {
                if (!Character.isDigit(s.charAt(i)) || s.charAt(i) == '.') {
                    return false;
                }
            }

            return true;
        }
    }

    public static void main(String[] args) {
        Solution solution = new Solution();

        String sentence = "706hzu76jjh7yufr5x9ot60v149k5 $7651913186000 pw2o $6";
        int discount = 28;

        String s = solution.discountPrices(sentence, discount);

        System.out.println(s);
    }
}
